<?php
class ModelsController extends AppController {

	var $name = 'Models';

	function index() {
            $this->set('title_for_layout', 'Vehículos - Modelos');
		$this->Model->recursive = 0;
		$this->set('models', $this->paginate());
                $this->loadModel('Brand');     
                $brands = $this->Brand->find('list');
                $this->set('brands',$brands);
         
	}

	function view($id = null) {
               $this->set('title_for_layout', 'Vehículos - Modelos');
		if (!$id) {
			$this->Session->setFlash(__('Invalid model', true));
			$this->redirect(array('action' => 'index'));
		}
		$this->set('model', $this->Model->read(null, $id));
                $this->loadModel('Brand');     
                $brands = $this->Brand->find('list');
                $this->set('brands',$brands);
	}

	function add() { 
               $this->set('title_for_layout', 'Vehículos - Modelos');
		if (!empty($this->data)) {
			$this->Model->create();
			if ($this->Model->save($this->data)) {
				$this->Session->setFlash(__('The model has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The model could not be saved. Please, try again.', true));
			}
		}
            $this->loadModel('Brand');     
            $brands = $this->Brand->find('list');
            $this->set('brands',$brands);
	}

	function edit($id = null) {
               $this->set('title_for_layout', 'Vehículos - Modelos');
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid model', true));
			$this->redirect(array('action' => 'index'));
		}
		if (!empty($this->data)) {
			if ($this->Model->save($this->data)) {
				$this->Session->setFlash(__('The model has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The model could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
                       $this->loadModel('Brand');     
                       $brands = $this->Brand->find('list');
                       $this->set('brands',$brands);
		       $this->data = $this->Model->read(null, $id);
		}
	}

	function delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Id Invalido', true));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->Model->delete($id)) {
			$this->Session->setFlash(__('Modelo borrado', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->Session->setFlash(__('El modelo no fue borrado', true));
		$this->redirect(array('action' => 'index'));
	}
}
